package com.ruoyi.swla2.service.impl;

import java.util.*;

import com.ruoyi.common.config.MinioConfigCommon;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.swla2.bo.DeviceLedgerBo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.swla2.mapper.DeviceLedgerMapper;
import com.ruoyi.swla2.domain.DeviceLedger;
import com.ruoyi.swla2.service.IDeviceLedgerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
/**
 * 设备台账Service业务层处理
 *
 * @author gdqt
 * @date 2025-06-26
 */
@Service
public class DeviceLedgerServiceImpl extends ServiceImpl<DeviceLedgerMapper, DeviceLedger> implements IDeviceLedgerService
{
    @Autowired
    private DeviceLedgerMapper deviceLedgerMapper;

    /**
     * 查询设备台账
     *
     * @param id 设备台账主键
     * @return 设备台账
     */
    @Override
    public DeviceLedger selectDeviceLedgerById(Long id)
    {

        return deviceLedgerMapper.selectById(id);
    }

    /**
     * 查询设备台账列表
     *
     * @param deviceLedger 设备台账
     * @return 设备台账
     */
    @Override
    public List<DeviceLedger> selectDeviceLedgerList(DeviceLedger deviceLedger)
    {
        LambdaQueryWrapper<DeviceLedger> queryWrapper = new LambdaQueryWrapper<>();
        if(deviceLedger.getReportDay() != null){
            queryWrapper.eq(DeviceLedger::getReportDay,deviceLedger.getReportDay());
        }
        if(!StringUtils.isEmpty(deviceLedger.getDeviceNum())){
            queryWrapper.eq(DeviceLedger::getDeviceNum,deviceLedger.getDeviceNum());
        }
        if(!StringUtils.isEmpty(deviceLedger.getStatus())){
            queryWrapper.eq(DeviceLedger::getStatus,deviceLedger.getStatus());
        }
        if(!StringUtils.isEmpty(deviceLedger.getLongitude())){
            queryWrapper.eq(DeviceLedger::getLongitude,deviceLedger.getLongitude());
        }
        if(!StringUtils.isEmpty(deviceLedger.getLatitude())){
            queryWrapper.eq(DeviceLedger::getLatitude,deviceLedger.getLatitude());
        }
        if(!StringUtils.isEmpty(deviceLedger.getRegion())){
            queryWrapper.eq(DeviceLedger::getRegion,deviceLedger.getRegion());
        }
        if(deviceLedger.getInstallTime() != null){
            queryWrapper.eq(DeviceLedger::getInstallTime,deviceLedger.getInstallTime());
        }
        if(!StringUtils.isEmpty(deviceLedger.getInstallPosition())){
            queryWrapper.eq(DeviceLedger::getInstallPosition,deviceLedger.getInstallPosition());
        }
        if(!StringUtils.isEmpty(deviceLedger.getNoiseReference())){
            queryWrapper.eq(DeviceLedger::getNoiseReference,deviceLedger.getNoiseReference());
        }
        return deviceLedgerMapper.selectList(queryWrapper);
    }

    @Override
    public List<DeviceLedger> listForGJAddLedger(DeviceLedgerBo deviceLedgerBo) {
        return deviceLedgerMapper.listForGJAddLedger(deviceLedgerBo);
    }

    @Override
    public List<DeviceLedger> listDeviceLedgerByMap(Map<String, Object> params) {
        return deviceLedgerMapper.listDeviceLedgerByMap(params);
    }



    @Override
    public List<DeviceLedgerBo> listDetailMore(DeviceLedgerBo deviceLedgerBo) {
        List<DeviceLedgerBo> result = deviceLedgerMapper.listDetailMore(deviceLedgerBo);
        if (CollectionUtils.isNotEmpty(result)){
            for (DeviceLedgerBo bo : result){
                if(!org.springframework.util.StringUtils.isEmpty(bo.getFileUrl())){
                    bo.setWavePreUrl(FileUploadUtils.getDownLoadFileUrl(bo.getFileUrl().substring(
                            MinioConfigCommon.getUrl().length() +MinioConfigCommon.getBucketName().length()  + 2
                    )));

                    bo.setFileNameBucket(bo.getFileUrl().substring(
                            MinioConfigCommon.getUrl().length() +MinioConfigCommon.getBucketName().length()  + 2
                    ));
                }
            }
        }
        return  result;
    }

    /**
     * 新增设备台账
     *
     * @param deviceLedger 设备台账
     * @return 结果
     */
    @Override
    public int insertDeviceLedger(DeviceLedger deviceLedger)
    {
        return deviceLedgerMapper.insert(deviceLedger);
    }

    /**
     * 修改设备台账
     *
     * @param deviceLedger 设备台账
     * @return 结果
     */
    @Override
    public int updateDeviceLedger(DeviceLedger deviceLedger)
    {
        deviceLedger.setUpdateTime(DateUtils.getNowDate());
        return deviceLedgerMapper.updateById(deviceLedger);
    }

    /**
     * 批量删除设备台账
     *
     * @param ids 需要删除的设备台账主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviceLedgerByIds(Long[] ids)
    {
        return deviceLedgerMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除设备台账信息
     *
     * @param id 设备台账主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteDeviceLedgerById(Long id)
    {
        return deviceLedgerMapper.deleteById(id);
    }
}
